package com.kitten.count.consumer;

import com.kitten.count.constant.MQConstants;
import com.kitten.count.constant.RedisKeyConstants;
import com.kitten.count.domain.mapper.NoteCountDOMapper;
import com.kitten.count.domain.mapper.UserCountDOMapper;
import com.kitten.count.model.dto.CountNoteOperateMqDTO;
import com.kitten.framework.common.utils.JsonUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author kitten
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "kitten_group_" + MQConstants.TOPIC_NOTE_OPERATE,
    topic = MQConstants.TOPIC_NOTE_OPERATE
)
public class CountNoteOperateConsumer implements RocketMQListener<Message> {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private UserCountDOMapper userCountDOMapper;
    @Autowired
    private NoteCountDOMapper noteCountDOMapper;

    @Override
    public void onMessage(Message message) {
        String bodyStr = new String(message.getBody());

        String tags = message.getTags();

        log.info(" 用户发布笔记数目 计数 Consumer Received message: {}, tags: {}", bodyStr, tags);

        if (Objects.equals(tags, MQConstants.TAG_NOTE_PUBLISH)) {
            handleNotePublish(bodyStr);
        } else if (Objects.equals(tags, MQConstants.TAG_NOTE_DELETE)) {
            handleNoteDelete(bodyStr);
        }

    }

    /**
     * 处理笔记发布事件计数
     * @param bodyStr
     */
    private void handleNotePublish(String bodyStr) {
        CountNoteOperateMqDTO dto = JsonUtils.parseObject(bodyStr, CountNoteOperateMqDTO.class);
        if (Objects.isNull(dto)) {
            return;
        }
        Long creatorId = dto.getCreatorId();
        // 计数用户的笔记数
        String countUserRedisKey = RedisKeyConstants.buildCountUserKey(creatorId);
        Boolean isExists = redisTemplate.hasKey(countUserRedisKey);

        if (isExists) {
            redisTemplate.opsForHash().increment(countUserRedisKey, RedisKeyConstants.FIELD_NOTE_TOTAL, 1);
        }

        userCountDOMapper.insertOrUpdateNoteTotalByUserId(creatorId, 1);
    }

    /**
     * 处理笔记删除事件计数
     * @param bodyStr
     */
    private void handleNoteDelete(String bodyStr) {
        CountNoteOperateMqDTO dto = JsonUtils.parseObject(bodyStr, CountNoteOperateMqDTO.class);
        if (Objects.isNull(dto)) {
            return;
        }
        Long creatorId = dto.getCreatorId();
        // 计数用户的笔记数
        String countUserRedisKey = RedisKeyConstants.buildCountUserKey(creatorId);
        Boolean isExists = redisTemplate.hasKey(countUserRedisKey);

        if (isExists) {
            redisTemplate.opsForHash().increment(countUserRedisKey, RedisKeyConstants.FIELD_NOTE_TOTAL, 1);
        }

        userCountDOMapper.insertOrUpdateNoteTotalByUserId(creatorId, -1);
    }

}
